<?php

namespace app\admin\controller\finance;

use app\admin\model\UM as UM;
use app\admin\service\SourceBillExportService;
use app\common\model\AccountBill;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use response\Excel;
use think\APP;
use think\Collection;
use think\Request as Request;
use think\Response;

/**
 * SourceBillExport 控制器
 * Class SourceBillExport
 * @package app\admin\controller\finance;
 * @link https://www.kancloud.cn/manual/thinkphp6_0/1037509
 * @see \app\admin\validate\finance\SourceBillExport
 */
class SourceBillExport
{
    // 请求参数
    private Request $request;

    // service
    private SourceBillExportService $service;

    public function __construct(Request $request, SourceBillExportService $sourceBillExportService)
    {
        $this->request = $request;
        $this->service = $sourceBillExportService;
    }

    /**
     * 充值消费清单-导出
     * @param UM|null $user
     * @return Response
     * @url /finance/source_bill_export/index
     */
    public function index(?UM $user): Response
    {
        $request = $this->request;

        $startTime = $request->param('startTime', null);
        $endTime = $request->param('endTime', null);

        if (is_blank($startTime)) {
            $startTime = null;
        }

        if (is_blank($endTime)) {
            $endTime = null;
        }

        $collection = $this->service->index($user, $startTime, $endTime);

        $spreadsheet = new Spreadsheet();

        // 填数据
        $this->change($spreadsheet, $collection);

        $filename = '充值消费清单';

        return new Excel($spreadsheet, $filename);
    }

    // 填数据
    private function change(Spreadsheet $spreadsheet, Collection $collection): void
    {
        $sheet = $spreadsheet->getActiveSheet();

        //表头
        $i = 1;
        $sheet->setCellValue('A' . $i, 'UID')
            ->setCellValue('B' . $i, '律师')
            ->setCellValue('C' . $i, '公司')
            ->setCellValue('D' . $i, '时间')
            ->setCellValue('E' . $i, '余额')
            ->setCellValue('F' . $i, '获客卡余额')
            ->setCellValue('G' . $i, '通讯币余额')
            ->setCellValue('H' . $i, '累计充值金额')
            ->setCellValue('I' . $i, '累计获客卡消费数量')
            ->setCellValue('J' . $i, '累计通讯币消费数量')
            ->setCellValue('K' . $i, '充值金额')
            ->setCellValue('L' . $i, '充值获客卡数量')
            ->setCellValue('M' . $i, '充值通信币数量')
            ->setCellValue('N' . $i, '消费获客卡数量')
            ->setCellValue('O' . $i, '消费通信币数量');

        //  UID	律师（公司）	时间	余额	获客卡余额	通讯比余额	累计充值金额	累计获客卡消费数量	累计通讯比消费数量	充值金额	充值获客卡数量	充值通信币数量	消费获客卡数量	消费通信币数量

        // 数据
        $collection->each(function (AccountBill $bill, $key) use ($sheet, &$i) {
            $i++;

            $other = $bill->other;
            // 被代付用户
            $proxyMember = $bill->proxyMember ?? null;


            $name = '';
            $company = '';

            // 是否是公司
            if ($bill->fromType == 'company' || $bill->toType == 'company') {
                if ($proxyMember) {
                    // 公司代付
                    $uid = $proxyMember->id;
                    $name = $proxyMember->showName;
                    $company = $other->name;
                } else {
                    // 公司
                    $uid = $other->id;
                    $company = $other->name;
                }
            } else {
                // 用户
                $uid = $other->id;
                $name = $other->showName;
            }

            $timeStr = date('Y-m-d H:i:s', $bill->created);

            // 余额	获客卡余额	通讯比余额	累计充值金额	累计获客卡消费数量	累计通讯比消费数
            $accountBalance = $other->account->balance;
            $cardBalance = $other->card->balance;
            $coinBalance = $other->coin->balance;
            if ($bill->fromType == 'company' || $bill->toType == 'company') {
                $rechargeSum = $other->transaction_offline_sum;
            } else {
                $rechargeSum = $other->token_recharge_trades_sum;
            }
            $cardSum = $other->cardTotalSum;
            $coinSum = $other->coinTotalSum;

            // 充值金额	充值获客卡数量	充值通信币数量	消费获客卡数量	消费通信币数量
            $money0 = '';
            $card0 = '';
            $coin0 = '';
            $card1 = '';
            $coin1 = '';

            // 判断是不是充值的支付流水（客户端充值，管理系统对公转账）
            if ($bill->billType == TRADE_TOKEN_RECHARGE || $bill->billType == TRADE_OFFLINE_CARD_RECHARGE || $bill->billType == TRADE_OFFLINE_COIN_RECHARGE) {
                $money0 = $bill->money;
            } else {
                // 获客卡 & 通信币的 充值和消费
                if ($bill->fromType == 'system') {
                    //充值

                    if ($bill->accountType == PAYMENT_CARD) {

                        $card0 = $bill->money;
                    } elseif ($bill->accountType == PAYMENT_COIN) {

                        $coin0 = $bill->money;
                    }
                } else {
                    // 消费

                    if ($bill->accountType == PAYMENT_CARD) {

                        $card1 = $bill->money;
                    } elseif ($bill->accountType == PAYMENT_COIN) {

                        $coin1 = $bill->money;
                    }
                }
            }

            $sheet->setCellValue('A' . $i, $uid)
                ->setCellValue('B' . $i, $name)
                ->setCellValue('C' . $i, $company)
                ->setCellValue('D' . $i, $timeStr)
                ->setCellValue('E' . $i, $accountBalance)
                ->setCellValue('F' . $i, $cardBalance)
                ->setCellValue('G' . $i, $coinBalance)
                ->setCellValue('H' . $i, $rechargeSum)
                ->setCellValue('I' . $i, $cardSum)
                ->setCellValue('J' . $i, $coinSum)
                ->setCellValue('K' . $i, $money0)
                ->setCellValue('L' . $i, $card0)
                ->setCellValue('M' . $i, $coin0)
                ->setCellValue('N' . $i, $card1)
                ->setCellValue('O' . $i, $coin1);
        });


    }
}
